package com.mbc.web.common.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.Currency;
import com.mbc.common.entities.CurrencyRpt;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.FrmCurrency;
import com.mbc.web.common.client.ui.Modal;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.MTextBox;
import com.mbc.web.common.shared.HttpStatus;

public class FrmCurrencyRpt extends Composite {

	/* MTextBox */
	@UiField TextBox txtfromcurrencyID;
	@UiField MTextBox txttocurrencyID;

	/* MButtons */
	@UiField MButton btnClear;
	@UiField MButton btnPreview;
	@UiField Button btnfromSearch;
	@UiField Button btntoSearch;

	private static FrmCurrencyRptUiBinder uiBinder = GWT
			.create(FrmCurrencyRptUiBinder.class);

	interface FrmCurrencyRptUiBinder extends UiBinder<Widget, FrmCurrencyRpt> {
	}

	public FrmCurrencyRpt() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}

	private void initialize() {
		
		txtfromcurrencyID.getElement().setAttribute("style", "text-transform: uppercase;");
		txttocurrencyID.getElement().setAttribute("style", "text-transform: uppercase;");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				txtfromcurrencyID.setFocus(true);
			}
		});
		txttocurrencyID.addKeyDownHandler(new KeyDownHandler() {

			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					event.preventDefault();
					TextBox l_txt = (TextBox) event.getSource();
					if (l_txt == txttocurrencyID)
						btnPreview.setFocus(true);
				}
			}
		});

	}

	private CurrencyRpt bindCurrencyRpt() {
		CurrencyRpt l_entity = new CurrencyRpt();
		l_entity.setFromCurrencyID(txtfromcurrencyID.getText());
		l_entity.setToCurrencyID(txttocurrencyID.getText());
		return l_entity;
	}

	@UiHandler("btnPreview")
	void onBtnPreviewClick(ClickEvent event) {
		ViewUtils.closeMessageBox();
	//	if(ViewUtils.isValidate()){
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.prepareCurrencyReport(bindCurrencyRpt(),
					currencyReportCallback());
	//	}
	}

	private AsyncCallback<Integer> currencyReportCallback() {
		return new AsyncCallback<Integer>() {

			@Override
			public void onSuccess(Integer result) {
				ViewUtils.stopAjaxLoader();
				if (result == HttpStatus.OK) {
					ClientSingleton.viewReport("Currency List");
				} else if (result == HttpStatus.NOT_FOUND) {
					ViewUtils.showErrorMessage("No records.");
				}
			}

			@Override
			public void onFailure(Throwable caught) {

			}
		};
	}

	@UiHandler("btnfromSearch")
	void onbtnfromSearchClick(ClickEvent event) {
		final SearchCurrency l_dia = new SearchCurrency("Currency List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					txtfromcurrencyID.setText(l_dia.getSelectedEntity().getCurrencyCode());
					txttocurrencyID.setFocus(true);
				} else
					txtfromcurrencyID.setFocus(true);
			}
		});
	}

	@UiHandler("btntoSearch")
	void onbtntoSearchClick(ClickEvent event) {
		final SearchCurrency l_dia = new SearchCurrency("Currency List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					txttocurrencyID.setText(l_dia.getSelectedEntity().getCurrencyCode());
				} else
					txttocurrencyID.setFocus(true);
			}
		});
	}

	@UiHandler("btnClear")
	void onBtnClearClick(ClickEvent event) {
		txttocurrencyID.setText("");
		txtfromcurrencyID.setText("");
		txtfromcurrencyID.setFocus(true);
	}
	@UiHandler("txtfromcurrencyID")
	void ontxtfromcurrencyIDChange(ChangeEvent event) {

		if (!txtfromcurrencyID.getText().equals("")){
			Currency l_entity = new Currency();
			l_entity.setCurrencyCode(txtfromcurrencyID.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyByCode(txtfromcurrencyID.getText(),  fromcurrencyIDCallback());
					
		}
	}
	private AsyncCallback<Currency> fromcurrencyIDCallback() {
		return new AsyncCallback<Currency>() {

			@Override
			public void onSuccess(Currency result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					ViewUtils.closeMessageBox();	
					txtfromcurrencyID.setText(result.getCurrencyCode());
					txtfromcurrencyID.getElement().setAttribute("id", String.valueOf(result.getCurrencyId()));
				} else {
					ViewUtils
							.showErrorMessage("Currency Code does not exist.");
					txtfromcurrencyID.setText("");
					txtfromcurrencyID.setFocus(true);
				}
			}
			@Override
			public void onFailure(Throwable caught) {
			}
		};
	}
	@UiHandler("txttocurrencyID")
	void ontxttocurrencyIDChange(ChangeEvent event) {

		if (!txttocurrencyID.getText().equals("")){
			Currency l_entity = new Currency();
			l_entity.setCurrencyCode(txttocurrencyID.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyByCode(txttocurrencyID.getText(),  tocurrencyIDCallback());
					
		}
	}
	private AsyncCallback<Currency> tocurrencyIDCallback() {
		return new AsyncCallback<Currency>() {

			@Override
			public void onSuccess(Currency result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					ViewUtils.closeMessageBox();	
					txttocurrencyID.setText(result.getCurrencyCode());
					txttocurrencyID.getElement().setAttribute("id", String.valueOf(result.getCurrencyId()));
				} else {
					ViewUtils
							.showErrorMessage("Currency Code does not exist.");
					txttocurrencyID.setText("");
					txttocurrencyID.setFocus(true);
				}
			}
			@Override
			public void onFailure(Throwable caught) {
			}
		};
	}
	
	@UiHandler("txtfromcurrencyID")
	void onTxtfromCurrencyKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onbtnfromSearchClick(null);
		}
	}
	
	@UiHandler("txttocurrencyID")
	void onTxttoCurrencyKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onbtntoSearchClick(null);
		}
	}
}
